package com.lianxian.xindong.home.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.hxb.library.base.BaseLazyLoadFragment;
import com.hxb.library.base.DefaultAdapter;
import com.hxb.library.base.MessageEvent;
import com.hxb.library.di.component.AppComponent;
import com.hxb.library.utils.AntiShakeUtils;
import com.hxb.library.utils.HxbUtils;
import com.lianxian.xindong.home.R;
import com.lianxian.xindong.home.R2;
import com.lianxian.xindong.home.di.component.DaggerCircleComponent;
import com.lianxian.xindong.home.mvp.contract.CircleContract;
import com.lianxian.xindong.home.mvp.presenter.CirclePresenter;
import com.lianxian.xindong.home.mvp.ui.activity.CirclePublishActivity;
import com.paginate.Paginate;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.hxb.library.utils.Preconditions.checkNotNull;


/**
 * @作者：hexingbo
 * @时间：07/04/2023
 * @描述：首页动态
 */
public class CircleFragment extends BaseLazyLoadFragment<CirclePresenter> implements CircleContract.View, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.refresh_layout)
    SwipeRefreshLayout mRefreshLayout;

    @Inject
    RecyclerView.LayoutManager mLayoutManager;
    @Inject
    RecyclerView.Adapter mAdapter;

    private Paginate mPaginate;
    private boolean isLoadingMore;

    public static CircleFragment newInstance() {
        CircleFragment fragment = new CircleFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerCircleComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_circle, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        initRecyclerView();
        initPaginate();
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {
        mRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        HxbUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        HxbUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public void onDestroyView() {
        DefaultAdapter.releaseAllHolder(mRecyclerView);//super.onDestroy()之后会unbind,所有view被置为null,所以必须在之前调用
        super.onDestroyView();
        this.mPaginate = null;
    }

    @Override
    public void onRefresh() {
        mPresenter.requestDatas(true);
    }

    @Override
    public void initRecyclerView() {
        mRefreshLayout.setOnRefreshListener(this);
        HxbUtils.configRecyclerView(mRecyclerView, mLayoutManager);
//        mRecyclerView.addItemDecoration(new SpacesItemDecorationLinearV(HxbUtils.dip2px(getContext(), 10)));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void initPaginate() {
        if (mPaginate == null) {
            Paginate.Callbacks callbacks = new Paginate.Callbacks() {
                @Override
                public void onLoadMore() {
                    //在需要加载数据的时候会调用onLoadMore()方法
                    mPresenter.requestDatas(false);
                }

                @Override
                public boolean isLoading() {
                    //则用于判断是否正在进行加载，若正处于加载状态中则返回true，否则返回false
                    return isLoadingMore;
                }

                @Override
                public boolean hasLoadedAllItems() {
                    //则用于判断是否还有需要加载的数据，若已经加载了全部的数据则返回true，否则返回false。
                    return mPresenter.hasLoadedAllItems();
                }
            };

            mPaginate = Paginate.with(mRecyclerView, callbacks)
                    .setLoadingTriggerThreshold(0)
                    .build();
            mPaginate.setHasMoreDataToLoad(false);
        }
    }

    /**
     * 开始加载更多
     */
    @Override
    public void startLoadMore() {
        isLoadingMore = true;
    }

    /**
     * 结束加载更多
     */
    @Override
    public void endLoadMore() {
        isLoadingMore = false;
    }


    @Override
    protected void lazyLoadData() {
        onRefresh();
    }

    @Override
    protected void onMessageEvent(MessageEvent message) {
        super.onMessageEvent(message);

    }

    @OnClick(R2.id.iv_circle_publish)
    public void onViewClicked(View view) {
        if (AntiShakeUtils.isInvalidClick(view)) {
            return;
        }
        launchActivity(new Intent(getContext(), CirclePublishActivity.class));
    }
}